package com.example.block_chain.controller.blockChain;

import com.example.block_chain.RabbitMQ.Producer;
import com.example.block_chain.common.AppHttpCodeEnum;
import com.example.block_chain.common.ResponseResult;
import com.example.block_chain.domain.pbft.VoteInfo;
import com.example.block_chain.service.blockChain.BlockService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/*
 *@description: 按学校多级组织多分支区块链接口
 *@author: hisen
 *@Time: 2024/2/15  22:11
 */
@RestController
public class OrganizationController {
    @Resource
    BlockService blockService;

    @Resource
    Producer producer;

    @PostMapping("/block/addSchool")
    public ResponseResult addSchool(@RequestParam String schoolCode) {
        if (blockService.haveCode(schoolCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CODE_EXISTS);
        }
        VoteInfo voteInfo = blockService.createSchoolBlock(schoolCode);
        producer.sendMessage(voteInfo);
        return ResponseResult.okResult();
    }

    @PostMapping("/block/addFaculty")
    public ResponseResult addFaculty(@RequestParam String schoolCode, @RequestParam String facultyCode) {
        if (blockService.haveCode(facultyCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CODE_EXISTS);
        }
        VoteInfo voteInfo = blockService.createFacultyBlock(schoolCode, facultyCode);
        producer.sendMessage(voteInfo);
        return ResponseResult.okResult();
    }

    @PostMapping("/block/addMajor")
    public ResponseResult addMajor(@RequestParam String facultyCode, @RequestParam String majorCode) {
        if (blockService.haveCode(majorCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CODE_EXISTS);
        }
        VoteInfo voteInfo = blockService.createMajorBlock(facultyCode, majorCode);
        producer.sendMessage(voteInfo);
        return ResponseResult.okResult();
    }

    @PostMapping("/block/addStudent")
    public ResponseResult addStudent(@RequestParam String sNo, @RequestParam String schoolCode, @RequestParam String majorCode) {
        if (blockService.haveCode(schoolCode + ":" + sNo)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CODE_EXISTS);
        }
        VoteInfo voteInfo = blockService.createStudentBlock(sNo, schoolCode, majorCode);
        producer.sendMessage(voteInfo);
        return ResponseResult.okResult();
    }
}
